package day01;

import com.mysql.jdbc.Driver;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class JDBCDemo {
        @Test
        public void method() throws Exception
            {
            //1）注册数据库驱动;
            DriverManager.registerDriver(new Driver());
            //jdbc:mysql: 固定书写格式  localhost数据库ip地址 3306 数据库端口号 day04_db数据库名
            String url="jdbc:mysql://localhost:3306/day04_db";//连接数据库
            String user="root";//登录数据库的用户名
            String password="1234";//登录数据库的密码
            //2）获取数据库连接;
            Connection conn = DriverManager.getConnection(url, user, password);
            //3）创建发送sql的statement对象;
            Statement st = conn.createStatement();
            //4）执行sql语句，并且获取返回结果;
            String sql="select *from user";
            ResultSet rs = st.executeQuery(sql);
            //5）遍历结果集;
            while(rs.next())
            {
                //获取id
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String pwd = rs.getString("password");
                //输出数据
                System.out.println(id+"=="+username+"=="+pwd);
                System.out.println("succesful");
                System.out.println("failed");
                System.out.println("erro");
                System.out.println("debug");
                System.out.println("info");

            }
            //6）释放资源;
            rs.close();
            st.close();
            conn.close();
        }
    }

